package Game.GameElements 
{
	import Box2D.Dynamics.b2Body;
	import Common.Animation;
	import Common.GamePhysics;
	import Common.Managers.SwfManager;
	import Common.Utils;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import Game.GameStates.GameState;
	/**
	 * ...
	 * @author Yevgeniy Logachev
	 */
	public class GameObject extends MovieClip
	{
		private static var m_gameObjects:Vector.<GameObject>		= new Vector.<GameObject>();
		
		private var 	m_objectSprite:MovieClip	= null;
		
		private var 	m_swfPath:String 			= null;
		private var 	m_symbArray:Array 			= null;
		public  var 	m_indxObj:int				= 0;
		
		private var 	m_physicalBody:b2Body		= null;
		
		public var 		isFreeze:Boolean			= false;
		public var 		isDestroyed:Boolean			= false;
		public var 		isEnemy:Boolean				= false;
		
		public function GameObject(startX:int, startY:int, startRot:int, swfPath:String, symbArray:Array, indxObj:int, isPhysicalObject:Boolean = true, isStatic:Boolean = true) 
		{
			super();
			
			x = startX;
			y = startY;
			rotation = startRot;
			
			m_swfPath = swfPath;
			m_symbArray = symbArray;
			m_indxObj = indxObj;
									
			SwfManager.instance.load(m_swfPath, onLoad);
			function onLoad():void
			{
				m_objectSprite = SwfManager.instance.getSprite(m_swfPath, m_symbArray[indxObj]) as MovieClip;//new Animation(SwfManager.instance.getSprite(m_swfPath, m_symbArray[indxObj]) as MovieClip, 30, 0, 0, true);
				m_objectSprite.stop();
				//m_objectSprite.cacheAsBitmap = true;
				addChild(m_objectSprite);
				if (isPhysicalObject)
				{
					m_physicalBody = GamePhysics.createPhysicalBody(startX, startY, startRot, m_objectSprite.parent, isStatic);
				}
			}
		}
		
		public function update():void
		{
			//m_objectSprite.update();
		}
		
		public static function create(data:ByteArray):GameObject
		{
			return null;
		}
		
		public function serialize(data:ByteArray):void
		{
		}
		
		public function release():void
		{
			if (m_physicalBody != null)
			{
				GamePhysics.instance.physicalWorld.DestroyBody(m_physicalBody);
				m_physicalBody = null;
			}
			if (m_objectSprite != null)
			{
				removeChild(m_objectSprite);
				m_objectSprite = null;
			}
			for (var i:int = 0; i < m_gameObjects.length; ++i)
			{
				if (m_gameObjects[i] as GameObject == this)
				{
					m_gameObjects.splice(i, 1);
				}
			}
			m_symbArray = null;
		}
		
		public function createPhysicalObject(isStatic:Boolean):void
		{
			if (m_physicalBody == null)
			{
				m_physicalBody = GamePhysics.createPhysicalBody(x, y, rotation, m_objectSprite.parent, isStatic);
			}
		}
		
		public function destroyPhysicalObject():void
		{
			if (m_physicalBody != null)
			{
				GamePhysics.instance.physicalWorld.DestroyBody(physicalObject);
				m_physicalBody = null;
			}
		}
		
		public function set Rotation(value:Number):void
		{
			if (m_physicalBody != null)
			{
				m_physicalBody.SetAngle(value * (Math.PI / 180.0));
			}
		}
		
		public function get Rotation():Number
		{
			if (m_physicalBody != null)
			{
				return m_physicalBody.GetAngle() * (180.0 / Math.PI);
			}
			
			return 0;
		}
		
		public function doDestroy():void
		{
			// override
		}
				
		public static function loadResource(swfPath:String, funcHandler:Function):void
		{
			SwfManager.instance.load(swfPath, funcHandler);
		}
		
		public static function isLoaded(swfPath:String):Boolean
		{
			return SwfManager.instance.isLoaded(swfPath);
		}		
		
		public function get objectSprite():MovieClip
		{
			return m_objectSprite;
		}
		
		public function get physicalObject():b2Body
		{
			return m_physicalBody;
		}
		
		public function set physicalObject(value:b2Body):void
		{
			m_physicalBody = value;
		}
		
		public static function get gameObjects():Vector.<GameObject>
		{
			return m_gameObjects;
		}
	}

}